// 店铺侧相关api
import { get, post, put, del } from '../common/ajax';
import { STO_STORE_URL, STO_SPOKESMAN_URL, STO_FANS_URL, STO_ACTIVITY_URL, STO_SETTLEMENT_URL, STO_AGGR_URL } from '../constants/url';
import { ACTIVITY_STATUS_VALUE, ACTIVITY_OWNER_VALUE } from '../constants/activity';
import { updateLocalMangeTargetInfo } from '../biztools/wex';
import { dealSpokesmanListData, dealSpokesmanRequestListData, dealFansListData } from '../biztools/manage';
import { dealActivityListData, dealActivityDetailData, dealActivitySaveData } from '../biztools/activity';
import { dealAccountLogListData, dealWithdrawLogListData } from '../biztools/record';
import { dealActivityStats, dealStatsConsignmentListData, dealSpokesmanRankListData } from '../biztools/stats';
import { setEmptyObj } from '../common/utils';

// 店铺相关

/**
 * 获取待开通的店铺列表
 */
export const getPreStoreList = async () => {
    const params = { pageNo: 1, pageSize: 100 };
    const res = await get(STO_STORE_URL.preList, params);
    return res.data ? res.data.rows : [];
};

/**
 * 开通店铺
 * @param {String} name 名称
 * @param {String} pic 头像
 * @param {String} intro 简介
 */
export const openStore = async (params) => {
    const res = await post(STO_STORE_URL.open, setEmptyObj(params));
    return res.data || false;
};

/**
 * 获取店铺列表
 */
export const getStoreList = async () => {
    const params = { pageNo: 1, pageSize: 100 };
    const res = await get(STO_STORE_URL.list, params);
    return res.data ? res.data.rows : [];
};

/**
 * 获取店铺信息(分场景)
 * @param {String} scene 场景
 */
export const getStoreInfo = async (params = {}) => {
    const { storeId } = params;
    const res = await get(STO_STORE_URL.storeInfo(storeId));
    let data = res.data || {};
    data.earningStats = {
        availableAmount: data.availableAmount,
        pendingAmount: data.pendingAmount,
        withdrawedAmount: data.withdrawedAmount,
        withdrawingAmount: data.withdrawingAmount,
    };
    return data;
};

/**
 * 修改店铺信息
 * @param {String} homepageBg 背景图
 * @param {String} name 名称
 * @param {String} intro 简介
 * @param {String} pic 头像
 * @param {String} tel 客服电话
 */
export const updateStoreInfo = async (params) => {
    const res = await put(STO_STORE_URL.updateInfo(), params);
    // 更新本地保存的用户信息
    updateLocalMangeTargetInfo(res.data);
    return res.data || false;
};

/**
 * 获取店铺的代言配置
 */
export const getEndorsement = async (storeId) => {
    const res = await get(STO_STORE_URL.getEndorsement(storeId));
    return res.data || {};
};

/**
 * 设置店铺的代言配置
 * @param {Object} params 
 */
export const setEndorsement = async (params) => {
    const res = await put(STO_STORE_URL.setEndorsement(), setEmptyObj(params));
    return res.data || false;
};

// 代言人相关

/**
 * 获取店铺代言人列表
 * @param {Number} pageSize 每页数据
 * @param {Number} pageNo 页数
 */
export const getSpokesmanList = async (params) => {
    params.pageSize = params.pageSize || 10;
    const res = await get(STO_SPOKESMAN_URL.list(), params);
    const { total = 0, rows = [] } = res.data || {};
    return { total, rows: dealSpokesmanListData(rows) };
};

/**
 * 店铺代言人备注
 * @param {Number|String} spnId 代言人id
 * @param {String} aliasName 备注名
 */
export const updateSpokesmanAlias = async (params) => {
    const { spnId, aliasName } = params;
    const res = await put(STO_SPOKESMAN_URL.alias(spnId), { aliasName });
    return res.data || false;
};

/**
 * 获取店铺代言人申请列表
 * @param {Number} pageSize 每页数据
 * @param {Number} pageNo 页数
 */
export const getSpokesmanRequestList = async (params) => {
    params.pageSize = params.pageSize || 10;
    const res = await get(STO_SPOKESMAN_URL.requestList(), params);
    const { total = 0, rows = [] } = res.data || {};
    return { total, rows: dealSpokesmanRequestListData(rows) };
};

/**
 * 店铺代言人申请审核
 * @param {String} type 类型 approve|reject
 * @param {Number|String} reqId 申请id
 */
export const auditSpokesmanRequest = async (type, reqId) => {
    const url = type === 'approve' ? STO_SPOKESMAN_URL.requestApprove(reqId) : STO_SPOKESMAN_URL.requestReject(reqId);
    const res = await post(url);
    return res.data || false;
};

// 店铺粉丝相关

/**
 * 获取店铺粉丝列表
 * @param {Number} pageSize 每页数据
 * @param {Number} pageNo 页数
 */
export const getFansList = async (params) => {
    params.pageSize = params.pageSize || 10;
    const res = await get(STO_FANS_URL.list(), params);
    const { total = 0, rows = [] } = res.data || {};
    return { total, rows: dealFansListData(rows) };
};

/**
 * 店铺粉丝备注
 * @param {Number|String} fansId 粉丝id
 * @param {String} aliasName 备注名
 */
export const updateFansAlias = async (params) => {
    const { fansId, aliasName } = params;
    const res = await put(STO_FANS_URL.alias(fansId), { aliasName });
    return res.data || false;
};

// 店铺收益、结算信息相关

/**
 * 获取结算信息
 */
export const getSettlementInfo = async () => {
    const res = await get(STO_SETTLEMENT_URL.detail());
    return res.data || {};
};

/**
 * 商家申请提现
 * @param {Number} amount 金额
 * @param {String} remark 备注
 */
export const applyWithdraw = async (params) => {
    const res = await post(STO_SETTLEMENT_URL.withdraw(), params);
    return res.data || false;
};

/**
 * 获取资金明细列表
 * @param {Number} pageSize 每页数据
 * @param {Number} pageNo 页数
 */
export const getAccountLogList = async (params) => {
    params.pageSize = params.pageSize || 10;
    const res = await get(STO_SETTLEMENT_URL.accountLogList(), params);
    const { total = 0, rows = [] } = res.data || {};
    return { total, rows: dealAccountLogListData(rows) };
};

/**
 * 获取提现记录明细
 * @param {Number} pageSize 每页数据
 * @param {Number} pageNo 页数
 */
export const getWithdrawLogList = async (params) => {
    params.pageSize = params.pageSize || 10;
    const res = await get(STO_SETTLEMENT_URL.withdrawLogList(), params);
    const { total = 0, rows = [] } = res.data || {};
    return { total, rows: dealWithdrawLogListData(rows) };
};

/**
 * 统计店铺的资金收入与支出
 * @param {Number} begin 开始时间
 * @param {Number} end 结束时间
 */
export const getAggrAccountLogsToInOut = async (params) => {
    const res = await get(STO_AGGR_URL.accountLogsToInOut(), params);
    return res.data || {};
};

// 活动相关

/**
 * 获取活动管理列表
 * @param {Number} pageSize 每页数据
 * @param {Number} pageNo 页数
 * @param {Object} ownerInfo
 */
export const getActivityList = async (params, ownerInfo) => {
    const res = await get(STO_ACTIVITY_URL.list(), params);
    return res.data && res.success ? dealActivityListData(res.data.rows, ownerInfo) : [];
};

// 获取活动复制列表(店铺创建)
export const getCopyStoreActivityList = async (params, ownerInfo) => {
    params.isAll = true;
    const res = await get(STO_ACTIVITY_URL.list(), params);
    const { total = 0, rows = [] } = res.data || {};
    return { total, rows: dealActivityListData(rows, ownerInfo) };
};

// 获取活动复制列表(品牌创建)
export const getCopyBrandActivityList = async (params) => {
    params.isAll = true;
    const res = await get(STO_ACTIVITY_URL.list(), params);
    const { total = 0, rows = [] } = res.data || {};
    return { total, rows: dealActivityListData(rows) };
};

/**
 * 获取活动复制信息(品牌创建活动)
 * @param {String} params
 */
export const getCopyBrandActivityDetail = async (params) => {
    const { storeId, activityId, isCopy = false } = params;
    const res = await get(STO_ACTIVITY_URL.detail(activityId, storeId));
    return res.data ? dealActivityDetailData(res.data, isCopy) : {};
};

/**
 * 获取活动信息
 * @param {String} params
 */
export const getActivityDetail = async (params) => {
    const { storeId, activityId, isCopy = false } = params;
    const res = await get(STO_ACTIVITY_URL.detail(activityId, storeId));
    return res.data ? dealActivityDetailData(res.data, isCopy) : {};
};

/**
 * 新建(编辑)活动
 */
export const editActivity = async (params) => {
    const { storeId, activityId } = params;
    params = dealActivitySaveData(params, ACTIVITY_OWNER_VALUE.store);
    const res = activityId ? await put(STO_ACTIVITY_URL.update(activityId, storeId), params) : await post(STO_ACTIVITY_URL.create(), params);
    return res.data || false;
};

/**
 * 发布活动
 * @param {String} params storeId 店铺id
 * @param {String} params activityId 活动id
 */
export const publishActivity = async (params) => {
    const { storeId, activityId } = params;
    const res = await put(STO_ACTIVITY_URL.publish(activityId, storeId));
    return res.data || false;
};

/**
 * 停用、启用活动
 * @param {String} params storeId 店铺id
 * @param {String} params activityId 活动id
 * @param {String} params processStatus 活动状态
 */
export const statusActivity = async (params) => {
    const { storeId, activityId, processStatus } = params;
    if (processStatus !== ACTIVITY_STATUS_VALUE.processing && processStatus != ACTIVITY_STATUS_VALUE.paused && processStatus !== ACTIVITY_STATUS_VALUE.starting) {
        return;
    }
    const url = processStatus === ACTIVITY_STATUS_VALUE.paused ? STO_ACTIVITY_URL.resume(activityId, storeId) : STO_ACTIVITY_URL.pause(activityId, storeId);
    const res = await put(url);
    return res.data || false;
};

/**
 * 活动数据统计=>收入
 * @param {String} params brandId 品牌id
 * @param {String} params activityId 活动id
 */
export const statsIncomeActivity = async (params) => {
    const { brandId, activityId } = params;
    const res = await get(STO_ACTIVITY_URL.statsIncome(activityId, brandId));
    return res.data ? dealActivityStats(ACTIVITY_OWNER_VALUE.brand, res.data) : {};
};

/**
 * 活动数据统计=>帮卖
 * @param {String} params brandId 品牌id
 * @param {String} params activityId 活动id
 */
export const statsConsignmentActivity = async (params) => {
    const { brandId, activityId } = params;
    const res = await get(STO_ACTIVITY_URL.statsConsignment(activityId, brandId));
    res.data && (res.data.spokesmanActivityConsignmentAggrInfos = dealStatsConsignmentListData(res.data.spokesmanActivityConsignmentAggrInfos));
    return res.data || {};
};

/**
 * 活动数据统计=>代言人排名
 * @param {String} params brandId 品牌id
 * @param {String} params activityId 活动id
 */
export const statsSpokesmanRankActivity = async (params) => {
    const { brandId, activityId, topRankNumber = 10 } = params;
    const res = await get(STO_ACTIVITY_URL.statsSpokesmanRank(activityId, brandId), { topRankNumber });
    return res.data ? dealSpokesmanRankListData(res.data) : {};
};
